package com.dotflat.xj.controller;


import com.dotflat.rest.controller.RestfulBaseController;
import com.dotflat.xj.domain.TargetRecord;
import com.dotflat.xj.po.TargetRecordPO;
import com.dotflat.xj.po.TargetTypeNamePO;
import com.dotflat.xj.po.TargetTypeRecordPO;
import com.dotflat.xj.po.TargetValueRecordAreaPO;
import com.dotflat.xj.service.TargetRecordService;
import com.dotflat.xj.service.TargetService;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.model.StatusResponse;
import com.matt.commons.page.PagerQuery;
import com.matt.domain.courseV2.po.AbilityPO;
import com.matt.service.remoteCall.SsoCallService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static com.matt.foundation.constant.RequestConstant.TICKET;

/**
 * 巡检记录Controller
 *
 * @author #
 * @date 2021-11-25
 */
@RestController
@RequestMapping("/api/xj_target_record")
@Slf4j
@Api(tags = "巡检 - 记录")
public class XJTargetRecordController extends RestfulBaseController<TargetRecord, TargetRecordService, TargetRecordPO> {

    @Autowired
    public void setBaseService(TargetRecordService service) {
        super.setBaseService(service);
    }


    @Autowired
    private TargetService targetService;

    @Autowired
    private TargetRecordService targetRecordService;

    @Autowired
    private SsoCallService ssoCallService;

    @ApiOperation(value = "详情")
    @PostMapping("/details")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "targetRecordId", value = "检查记录ID", required = true, dataType = "string")
    })
    public StatusResponse details(@RequestHeader(TICKET) String ticket, String targetRecordId){
        Map map = targetRecordService.details(ticket,targetRecordId);
        return StatusResponse.success(map);
    }

    @ApiOperation(value = "app 添加 - 巡检记录")
    @PostMapping("/add_record")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "targetRecord", value = "记录对象", required = false, dataTypeClass =TargetRecordPO.class )
    })
    public StatusResponse add(@RequestHeader(TICKET) String ticket,@RequestBody TargetRecordPO  targetRecord) {
        targetRecordService.add(ticket,targetRecord);
        return StatusResponse.success("添加成功");
    }

    @ApiOperation(value = "app 统计 ")
    @PostMapping("/census")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "targetId", value = "目标对象ID", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "time", value = "时间，不传查当天", required = false, dataType = "string")
    })
    public StatusResponse census(@RequestHeader(TICKET) String ticket, String  targetId,String time) {
        List<Map> m  = targetRecordService.census(ticket,targetId,time);
        return StatusResponse.success(m);
    }


    @ApiOperation(value = "集团 - 统计：天、周")
    @PostMapping("/group_count")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "type", value = "类型 0时间，1类型", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "week", value = "是否周查询", required = true, dataType = "boolean"),
            @ApiImplicitParam(paramType = "query", name = "time", value = "时间:不传未当前日期", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "sort", value = "大类别，分类统计不生效", required = false, dataType = "string")
    })
    public StatusResponse groupCount(@RequestHeader(TICKET) String ticket,int type,Boolean week,String time,String sort){
        List<Map> map = targetRecordService.groupCount(ticket,type,week,time,sort);
        return StatusResponse.success(map);
    }


    @ApiOperation(value = "巡检 - 总统计")
    @PostMapping("/total_count")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "startTime", value = "开始时间yyyy-MM-dd", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "endTime", value = "结束时间yyyy-MM-dd", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "sort", value = "大类别", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "areaId", value = "园所Id", required = false, dataType = "string")
    })
    public StatusResponse totalCount(@RequestHeader(TICKET) String ticket,String startTime,String endTime,String sort,String areaId){
        List<Map> map = targetRecordService.totalCount(ticket,startTime,endTime,sort,areaId);
        return StatusResponse.success(map);
    }


    @ApiOperation(value = "集团 - 总统计")
    @PostMapping("/group_totalCount")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "startTime", value = "开始时间yyyy-MM-dd", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "endTime", value = "结束时间yyyy-MM-dd", required = true, dataType = "string"),
    })
    public StatusResponse groupTotalCount(@RequestHeader(TICKET) String ticket,String startTime,String endTime){
        List<Map> map = targetRecordService.groupTotalCount(ticket,startTime,endTime);
        return StatusResponse.success(map);
    }



    @ApiOperation(value = "巡检 - 结果")
    @PostMapping("/count_result")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "startTime", value = "开始时间yyyy-MM-dd", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "endTime", value = "结束时间yyyy-MM-dd", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "type", value = "小类别", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "areaId", value = "园所Id", required = false, dataType = "string")
    })
    public StatusResponse countResult(@RequestHeader(TICKET) String ticket,String startTime,String endTime,String type,String areaId) throws Exception{
        List<Map> map = targetRecordService.countResult(ticket,startTime,endTime,type,areaId);
        return StatusResponse.success(map);
    }





    @ApiOperation(value = "巡检 - 结果 -详情")
    @PostMapping("/count_result_details")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "recordId", value = "记录ID", required = true, dataType = "string")
    })
    public StatusResponse countResultDetails(@RequestHeader(TICKET) String ticket,String recordId) {
        List<Map> map = targetRecordService.countResultDetails(ticket,recordId);
        return StatusResponse.success(map);
    }

    @ApiOperation(value = "园区大类型 - 总统计")
    @PostMapping("/area_type_count")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "areaId", value = "园区ID", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "startTime", value = "开始时间yyyy-MM-dd", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "endTime", value = "结束时间yyyy-MM-dd", required = true, dataType = "string"),
    })
    public StatusResponse areaTypeCount(@RequestHeader(TICKET) String ticket,String areaId, String startTime, String endTime){
        List<TargetTypeRecordPO> list = targetRecordService.areaTypeCount( ticket,  areaId,  startTime,  endTime);
        return StatusResponse.success(list);
    }

    @ApiOperation(value = "园区类型名称 - 总统计")
    @PostMapping("/area_type_name_count")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "areaId", value = "园区ID", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "startTime", value = "开始时间yyyy-MM-dd", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "endTime", value = "结束时间yyyy-MM-dd", required = true, dataType = "string"),
    })
    public StatusResponse areaTypeNameCount(@RequestHeader(TICKET) String ticket,String areaId, String startTime, String endTime){
        List<TargetTypeRecordPO> list = targetRecordService.typeNameCount( ticket,  areaId,  startTime,  endTime);
        return StatusResponse.success(list);
    }



    @ApiOperation(value = "巡检 - 园区数据")
    @PostMapping("/area_count")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "startTime", value = "开始时间yyyy-MM-dd", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "endTime", value = "结束时间yyyy-MM-dd", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "type", value = "小类别", required = false, dataType = "string"),
    })
    @ApiResponses({
            @ApiResponse(code = 000000, message = "successful", response = TargetValueRecordAreaPO.class),
    })
    public StatusResponse getAreaCountResult(@RequestHeader(TICKET) String ticket,String startTime,String endTime,String type, PagerQuery pagerQuery) throws Exception{
        StaffDTO staffDTO = ssoCallService.getStaffDTO(ticket);
        Page<TargetValueRecordAreaPO> map = targetRecordService.getAreaCountResult(staffDTO.getAreaId(),startTime,endTime,type,  pagerQuery);
        return StatusResponse.success(map);
    }


    @ApiOperation(value = "巡检ID状态")
    @PostMapping("/target_status")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "targetId", value = "目标对象ID", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "targetClassId", value = "巡检班级ID", required = true, dataType = "string"),
    })
    public StatusResponse targetStatus(@RequestHeader(TICKET) String ticket,String targetId,String targetClassId) {
        StaffDTO staffDTO = ssoCallService.getStaffDTO(ticket);
        //0未巡检，1已巡检
        TargetTypeNamePO po=  targetRecordService.targetStatus(staffDTO.getAreaId(),targetId,targetClassId);
        return StatusResponse.success(po);
    }

}